﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace ProtocolConversion_WINCE
{
    class GPIO
    {
        [DllImport("HDI_API.dll")]
        public static extern void API_GPIO_Output(byte channel, byte level);
        //[DllImport("HDI_API.dll")]
        //public static extern void API_GPIO_Input(byte channel, byte *level);

        //public delegate void OnCalculateTimeDelegate(int time);
        //public event OnCalculateTimeDelegate OnCalculateTime;

        Thread timeThread;

        private bool startCalTime = false;
        private UInt16 Device = 100;

        private bool isRestarting = false;
        private Form1 form1;

        //初始化GPIO
        public GPIO(Form1 form1)
        {
            this.form1 = form1;

            if (timeThread == null)
                timeThread = new Thread(TimeThread);
            timeThread.IsBackground = true;
            //timeThread.Start();
            startCalTime = false;
            API_GPIO_Output(0, 0);//地震采集器
            API_GPIO_Output(1, 0);//前兆采集器
            API_GPIO_Output(2, 0);//电科主机
            API_GPIO_Output(3, 0);//终端
            //timeThread.Start();
        }
        /// <summary>
        /// 重启设备
        /// </summary>
        /// <param name="device">设备编号</param>
        /// <param name="time">重启时间</param>
        /// <param name="level">电平</level>
        /// 低电平闭合，高电平断开
        public void RestartDevice(UInt16 device, byte level)
        {
            Device = device;

            switch (device)
            {
                //重启地震数据采集器(GZ)
                case 0x2001:
                    API_GPIO_Output(0, level);//断开继电器
                    if (level == 0)
                        break;
                    startCalTime = true;
                    try
                    {
                        timeThread.Start();
                    }
                    catch { };                    
                    break;
                //重启前兆数据采集器
                case 0x2002:
                    API_GPIO_Output(1, level);//断开继电器
                    if (level == 0)
                        break;
                    startCalTime = true;
                    try
                    {
                        timeThread.Start();
                    }
                    catch { }; 
                    break;
                //重启电科控制主机
                case 0x2003:
                    API_GPIO_Output(2, level);//断开继电器
                    if (level == 0)
                        break;
                    startCalTime = true;
                    try
                    {
                        timeThread.Start();
                    }
                    catch { }; 
                    break;
                //重启终端
                case 0x2004:
                    API_GPIO_Output(3, level);//断开继电器
                    if (level == 0)
                        break;
                    startCalTime = true;
                    try
                    {
                        timeThread.Start();
                    }
                    catch { }; 
                    break;
                //重启总控
                case 0x2005:
                    break;
                default: break;
            }
        }
        //开启线程计时
        private void TimeThread()
        {
            while (true)
            {
                if (startCalTime)
                {
                    UInt16 restartDevice = Device;
                    Thread.Sleep(20000);                    
                    this.RestartDevice(restartDevice, 0);//闭合继电器
                    switch (restartDevice)
                    {
                        //重启地震数据采集器(GZ)
                        case 0x2001:
                            form1.AppendString("地震数据采集器重启成功");
                            break;
                        //重启前兆数据采集器
                        case 0x2002:
                            form1.AppendString("前兆数据采集器重启成功");
                            break;
                        //重启电科控制主机
                        case 0x2003:
                            form1.AppendString("电科主机重启成功");
                            break;
                        default: break;
                    }
                    startCalTime = false;
                    //timeThread.Abort();                    
                }
                Thread.Sleep(20);
            }
        }
        public void CloseThread()
        {
            try
            {
                timeThread.Abort();
            }
            catch { }
        }
    }
}
